package cn.kzgg.pet.service.impl;

import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.util.BaiduAuditUtils;
import cn.kzgg.basic.util.CodeGenerateUtils;
import cn.kzgg.basic.util.DistanceUtil;
import cn.kzgg.basic.util.Point;
import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.domain.Shop;
import cn.kzgg.org.mapper.ShopMapper;
import cn.kzgg.pet.domain.OrderPetAcquisition;
import cn.kzgg.pet.domain.Pet;
import cn.kzgg.pet.domain.SearchMasterMsg;
import cn.kzgg.pet.domain.SearchMasterMsgAuditLog;
import cn.kzgg.pet.dto.ToHandleDto;
import cn.kzgg.pet.mapper.OrderPetAcquisitionMapper;
import cn.kzgg.pet.mapper.PetMapper;
import cn.kzgg.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.kzgg.pet.mapper.SearchMasterMsgMapper;
import cn.kzgg.pet.service.ISearchMasterMsgService;
import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.user.domain.Logininfo;
import cn.kzgg.user.domain.User;
import cn.kzgg.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-28
 */
@Service
@Slf4j
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg msg, Logininfo logininfo) {
        //1.获取寻主用户
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        //获取寻主信息
        msg.setUserId(user.getId());
        //保存寻主消息
        searchMasterMsgMapper.save(msg);
        //2.自动审核一些信息
        String str = msg.getAddress() + msg.getName() + msg.getTitle() + msg.getCoatColor();
        if(!BaiduAuditUtils.TextCensor(str)){
            //审核不通过记录日志并保存
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(msg.getId());
            log.setNote("请检查你输入的那些玩意儿，有敏感信息");
            searchMasterMsgAuditLogMapper.save(log);
            msg.setState(-1);
        }else {
            //通过了就修改状态并找最近的店铺
            msg.setState(1);
            //获取发布信息的地点
            Point point = DistanceUtil.getPoint(msg.getAddress());
            //根据这个点查找50公里内最近的店铺
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
            System.out.println(nearestShop);
            if(nearestShop!=null){
                //如果有就将信息绑定给店铺,并给店铺管理者发信息
                msg.setShopId(nearestShop.getId());
                log.info("生意上门啦请注意查收"+nearestShop.getName());

            }
        }
        searchMasterMsgMapper.update(msg);

    }

    /**
     * 接单
     * @param dto
     */
    @Override
    public void accept(ToHandleDto dto) {
        //判断是否被人接单了，根据msgId和没有被取消的状态
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadByMsgIdAndNoCancel(dto.getMsgId());
        if(order!=null){
            //说明被接了
            throw new BusinessException("单子被抢啦~~下次手快点");
        }
        //获取宠物信息
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(dto.getMsgId());
        //生成宠物
        Pet pet =inintPet(dto,msg);
        petMapper.save(pet);
        //生成订单
        order = initOrderPetAcquisition(dto, msg, pet);
        orderPetAcquisitionMapper.save(order);


    }

    /**
     * 拒收
     * @param id
     */
    @Override
    public void reject(Long id) {
        //获取要拒收的寻主信息
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(id);
        //将shopId置空
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);

    }

    private OrderPetAcquisition initOrderPetAcquisition(ToHandleDto dto, SearchMasterMsg msg, Pet pet) {
        OrderPetAcquisition order;
        order = new OrderPetAcquisition();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(dto.getHandler()));
        order.setDigest(pet.getName() + ":收购订单");
        order.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        order.setPetId(pet.getId());
        order.setUserId(msg.getUserId());
        order.setShopId(msg.getShopId());
        order.setAddress(msg.getAddress());
        //绑定收购人
        order.setEmpId(dto.getHandler());
        order.setSearchMasterMsgId(dto.getMsgId());
        return order;
    }


    //生成宠物实现方法
    private Pet inintPet(ToHandleDto dto, SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        //@TODO 在收购宠物回来之后，确认订单时，来填写成本价，上架之前来填写售价 包括补充不要宠物信息
        pet.setTypeId(msg.getPetType());
        pet.setShopId(msg.getShopId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;

    }
}
